// pages/sign/card/give/execute.js
var api = require("../../../../utils/api.js");
var util = require("../../../../utils/util.js");

const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: {},
    cardInfo: [],
    giveCardInfo: false,
    giveSuccessView: false,
  },

  intercept: function (e) {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      user: JSON.parse(options.userInfo),
    });
    this.updateCardInfo();
  },
  //更新赠送卡牌信息
  updateCardInfo: function() {
    // [{ "card_name": 0, "count": 1 }]
    // var cardInfo = [{ card_name: 3, type: 1, number: 1, count: 0 }, { card_name: 8, type: 2, number: 1, count: 0 }, { card_name: 9, type: 2, number: 1, count: 0}];
    var cardInfo = [];
    app.globalData.cardInfo.member_card.first.forEach(card => {
      if (card.number > 0) {
        card.type = 1;
        card.count = 0;
        cardInfo.push(card);
      }
    });
    app.globalData.cardInfo.member_card.end.forEach(card => {
      if (card.number > 0) {
        card.type = 2;
        card.count = 0;
        cardInfo.push(card);
      }
    });
    this.setData({
      cardInfo: cardInfo,
    });
    // console.log(this.data.cardInfo);
  },
  //监听更新
  change: function(e) {
    var currentCount = e.detail;
    var currentCard = e.currentTarget.dataset.card;
    var giveBool = false;
    // console.log(this.data.cardInfo)
    this.data.cardInfo.forEach(card => {
      if (card.card_name == currentCard.card_name) {
        card.count = currentCount;
      }
      if (card.count > 0) {
        giveBool = true;
      }
    });

    this.setData({
      giveCardInfo: giveBool,
      cardInfo: this.data.cardInfo,
    });
  },
  //赠送卡牌
  giveCard: function(e) {
    wx.showLoading({});
    var cardInfo = [];
    this.data.cardInfo.forEach(card=>{
      if(card.count > 0){
        cardInfo.push(card);
      }
    });
    api.sendCard(this.data.user.user_id, cardInfo, () => {
      wx.hideLoading();
      this.setData({
        giveSuccessView: true,
      });
    }, res => {
      wx.hideLoading();
      wx.showToast({
        title: res,
        icon: 'none'
      })
    });
  },
  //赠送成功
  giveSuccess: function(e) {
    this.setData({
      giveSuccessView: false,
    });
    wx.navigateBack({
      delta: 2,
    })
  },
  //隐藏赠送页面
  hideGiveView: function(e) {
    this.data.cardInfo.forEach(card => {
      if (card.type == 1) {
        app.globalData.cardInfo.member_card.first.forEach(card1 => {
          if (card1.card_name == card.card_name) {
            card1.number = card1.number - card.count;
          }
        });
      }else{
        app.globalData.cardInfo.member_card.end.forEach(card1 => {
          if (card1.card_name == card.card_name) {
            card1.number = card1.number - card.count;
          }
        });
      }
    });
    this.updateCardInfo();
    this.setData({
      giveSuccessView: false,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return app.shareSeekHelp();
  }
})